package com.chanjet.openapi.sdk.java;

import com.alibaba.fastjson.JSONArray;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.chanjet.openapi.sdk.java.utils.ChanjetLogger;
import com.chanjet.openapi.sdk.java.utils.HttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractChanjetClient implements ChanjetClient {

    private static final int CONNECT_TIMEOUT = 3000;
    private static final int READ_TIMEOUT = 15000;
    private static final int KEEP_ALIVE_TIMEOUT = 60;
    private final String serverUrl;
    private final int connectTimeout;
    private final int readTimeout;
    private String signKey;

    public AbstractChanjetClient(String serverUrl) {
        this(serverUrl, 3000, 15000, 60);
    }

    public AbstractChanjetClient(String serverUrl, String signKey) {
        this(serverUrl, 3000, 15000, 60, signKey);
    }

    public AbstractChanjetClient(String serverUrl, int connectTimeout, int readTimeout) {
        this(serverUrl, connectTimeout, readTimeout, 60);
    }

    public AbstractChanjetClient(String serverUrl, int connectTimeout, int readTimeout, int keepAliveTimeout) {
        this.serverUrl = serverUrl;
        this.connectTimeout = connectTimeout;
        this.readTimeout = readTimeout;
        HttpUtils.setKeepAliveTimeout(keepAliveTimeout);
    }

    public AbstractChanjetClient(String serverUrl, int connectTimeout, int readTimeout, int keepAliveTimeout, String signKey) {
        this.serverUrl = serverUrl;
        this.connectTimeout = connectTimeout;
        this.readTimeout = readTimeout;
        this.signKey = signKey;
        HttpUtils.setKeepAliveTimeout(keepAliveTimeout);
    }

    public <T extends ChanjetResponse> T execute(ChanjetRequest<T> request) throws ChanjetApiException {
        HttpResponse httpResponse = this.invoke(request);

        try {
            String body = httpResponse.getBody();
            if(body.startsWith("[")){
                Map map = new HashMap<>();

                map.put("data", JSONArray.parseArray(body));
                String str = new Gson().toJson(map);
                return (new Gson()).fromJson(str, request.getResponseClass());
            }else{
                return (new Gson()).fromJson(httpResponse.getBody(), request.getResponseClass());
            }
        } catch (Exception var4) {
            ChanjetLogger.logBizError(request.getResponseClass().toString() + "--响应实体转型错误", var4);
            throw new ChanjetApiException("响应实体转型错误", var4);
        }
    }

    private <T extends ChanjetResponse> HttpResponse invoke(ChanjetRequest<T> request) throws ChanjetApiException {
        try {
            ChanjetLogger.logBizCommonParam(request.getAppKey());
            ChanjetLogger.logHttpRequest(request.getHttpMethod().name(), this.serverUrl + request.getRequestUri(), request.getQueryParams(), request.getHeaders(), (new Gson()).toJson(request.getBizContent()));
            HttpResponse httpResponse;
            switch (request.getHttpMethod()) {
                case GET:
                    httpResponse = this.doGet(request);
                    break;
                case POST:
                    httpResponse = this.doPost(request);
                    break;
                case DELETE:
                    httpResponse = this.doDelete(request);
                    break;
                case PUT:
                    httpResponse = this.doPut(request);
                    break;
                default:
                    throw new ChanjetApiException();
            }

            ChanjetLogger.logHttpResponse(request.getHttpMethod().name(), this.serverUrl + request.getRequestUri(), request.getQueryParams(), httpResponse.getHeaders(), httpResponse.getBody());
            return httpResponse;
        } catch (IOException var4) {
            ChanjetLogger.logBizError("调用API失败", var4);
            throw new ChanjetApiException("调用API失败", var4);
        }
    }

    private <T extends ChanjetResponse> HttpResponse doPost(ChanjetRequest<T> request) throws IOException, ChanjetApiException {
        this.sign(request, this.signKey);
        String body = (new Gson()).toJson(request.getBizContent());
        return HttpUtils.doPost(this.serverUrl + request.getRequestUri(), request.getHeaders(), request.getQueryParams(), request.getContentType(), body, this.connectTimeout, this.readTimeout, (String)null, 0);
    }

    private <T extends ChanjetResponse> HttpResponse doGet(ChanjetRequest<T> request) throws IOException, ChanjetApiException {
        this.sign(request, this.signKey);
        return HttpUtils.doGet(this.serverUrl + request.getRequestUri(), request.getHeaders(), request.getQueryParams(), request.getContentType(), this.connectTimeout, this.readTimeout);
    }

    private <T extends ChanjetResponse> HttpResponse doDelete(ChanjetRequest<T> request) throws IOException, ChanjetApiException {
        this.sign(request, this.signKey);
        return HttpUtils.doDelete(this.serverUrl + request.getRequestUri(), request.getHeaders(), request.getQueryParams(), request.getContentType(), this.connectTimeout, this.readTimeout);
    }

    private <T extends ChanjetResponse> HttpResponse doPut(ChanjetRequest<T> request) throws IOException, ChanjetApiException {
        this.sign(request, this.signKey);
        String body = (new Gson()).toJson(request.getBizContent());
        return HttpUtils.doPut(this.serverUrl + request.getRequestUri(), request.getHeaders(), request.getQueryParams(), request.getContentType(), body, this.connectTimeout, this.readTimeout, (String)null, 0);
    }

    abstract void sign(ChanjetRequest var1, String var2) throws ChanjetApiException;
}
